import 'package:casdon/app/config/env/env_provider.dart';
import 'package:casdon/app_providers.dart';
import 'package:casdon/core/network/i_api_client.dart';
import 'package:casdon/modules/recipe/data/api/category_recipe_list_api.dart';
import 'package:casdon/modules/recipe/data/api/cook_model_api.dart';
import 'package:casdon/modules/recipe/data/api/cook_module_content_api.dart';
import 'package:casdon/modules/recipe/data/api/miffy_product_list_api.dart';
import 'package:casdon/modules/recipe/data/api/miffy_recipe_cloud_list_api.dart';
import 'package:casdon/modules/recipe/data/api/miffy_recipe_info_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_banner_list_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_collect_detail_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_detail_info_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_category_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_cloud_list_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_ecprotocol_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_favorite_list_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_module_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_module_content_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_product_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_search_list_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_today_api.dart';
import 'package:casdon/modules/recipe/data/api/recipe_user_info_api.dart';
import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';
import 'package:casdon/core/utils/json_parse_utils.dart';

class RecipeRepositoryImpl extends RecipeRepository {
  // 字段类型也是抽象接口
  final IApiClient _apiClient;

  RecipeRepositoryImpl({required IApiClient apiClient})
    : _apiClient = apiClient;

  @override
  Future<List<ResponseRecipeBannerListApi>?> getRecipeBannerList() async {
    final res = await _apiClient.getTyped<List<ResponseRecipeBannerListApi>>(
      '/client-operation/position/getBannerList',
      query: {"type": 2},
      fromJsonT: (json) =>
          parseList(json, ResponseRecipeBannerListApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeCategoryApi?> getRecipeCategory(
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeCategoryApi>(
      '/client-operation/recipe/platform/selectCategoryPage',
      query: {
        "pageSize": pageSize,
        "pageNum": pageNum,
        "orderByColumn": "sort",
        "isAsc": "asc",
        "status": 1,
        "nodeType": 1,
      },
      fromJsonT: (json) =>
          ResponseRecipeCategoryApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeCategoryApi?> getRecipeCategoryWithId(
    String id,
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeCategoryApi>(
      '/client-operation/recipe/platform/selectCategoryPage',
      query: {"pageSize": pageSize, "pageNum": pageNum, "id": id},
      fromJsonT: (json) =>
          ResponseRecipeCategoryApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<List<ResponseRecipeModuleApi>?> getRecipeModuleList() async {
    final res = await _apiClient.getTyped<List<ResponseRecipeModuleApi>>(
      '/client-operation/recipe/operation/getRecipeModuleList',
      query: {"statue": 1},
      fromJsonT: (json) => parseList(json, ResponseRecipeModuleApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeModuleContentApi?> getRecipeModuleContent(
    RequestRecipeModuleContentApi request,
  ) async {
    final res = await _apiClient.postTyped<ResponseRecipeModuleContentApi>(
      '/client-operation/recipe/operation/searchAggregate',
      data: request.toJson(),
      fromJsonT: (json) =>
          ResponseRecipeModuleContentApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeTodayApi?> getRecipeTodayList(
    int pageNum,
    int pageSize,
    String orderby,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeTodayApi>(
      '/client-operation/recipe/platform/getSelectionOfRecipes',
      query: {
        "pageSize": 20,
        "pageNum": pageNum,
        "isAsc": "desc",
        "orderByColumn": orderby,
        "isWeekMenu": false,
      },
      fromJsonT: (json) =>
          ResponseRecipeTodayApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeCloudListApi?> getRecipeCloudList(
    String pid,
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeCloudListApi?>(
      '/client-operation/recipe/platform/getRecipeCloudPageList',
      query: {
        "pid": pid,
        "pageSize": pageSize,
        "pageNum": pageNum,
        "isLocal": 0,
        "orderByColumn": "updateTime",
        "isAsc": "desc",
      },
      fromJsonT: (json) =>
          ResponseRecipeCloudListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeFavoriteListApi?> getRecipeFavoriteList(
    int pageNum,
    int pageSize,
    String orderByColumn,
    String isAsc,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeFavoriteListApi?>(
      '/client-operation/recipe/user/getUserCollect',
      query: {
        "pageSize": pageSize,
        "pageNum": pageNum,
        // "orderByColumn": orderByColumn,
        // "isAsc": isAsc,
      },
      fromJsonT: (json) =>
          ResponseRecipeFavoriteListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<int> putRecipeCollect(String menuId) async {
    final res = await _apiClient.putTyped<int>(
      '/client-operation/recipe/user/addUserCollect',
      data: {"menuId": menuId},
      fromJsonT: (json) {
        return json as int;
      },
    );
    return res?.data ?? 0;
  }

  @override
  Future<int> delRecipeCollect(String menuId) async {
    final res = await _apiClient.deleteTyped<int>(
      '/client-operation/recipe/user/deleteUserCollect/{menuId}',
      pathParams: {"menuId": menuId},
      fromJsonT: (json) {
        return json as int;
      },
    );
    return res?.data ?? 0;
  }

  @override
  Future<ResponseRecipeCloudListApi?> getRecipeLikeList(
    int pageNum,
    int pageSize,
    String orderByColumn,
    String isAsc,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeCloudListApi?>(
      '/client-operation/recipe/user/getUserLikes',
      query: {
        "pageSize": 20,
        "pageNum": 1,
        "orderByColumn": "id",
        "isAsc": "asc",
      },
      fromJsonT: (json) =>
          ResponseRecipeCloudListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<int> putRecipeLike(String menuId) async {
    final res = await _apiClient.putTyped<int>(
      '/client-operation/recipe/user/addUserLikes',
      data: {"menuId": menuId},
      fromJsonT: (json) {
        return json as int;
      },
    );
    return res?.data ?? 0;
  }

  @override
  Future<int> putRecipeBrower(String menuId) async {
    final res = await _apiClient.putTyped<int>(
      '/client-operation/recipe/user/addUserBrowses',
      data: {"menuId": menuId},
      fromJsonT: (json) {
        return json as int;
      },
    );
    return res?.data ?? 0;
  }

  @override
  Future<int> delRecipeLike(String menuId) async {
    final res = await _apiClient.deleteTyped<int>(
      '/client-operation/recipe/user/deleteUserLikes/{menuId}',
      pathParams: {"menuId": menuId},
      fromJsonT: (json) {
        return json as int;
      },
    );
    return res?.data ?? 0;
  }

  @override
  Future<RecipeUserInfoApi?> getUserRecipeInfo(String menuId) async {
    final res = await _apiClient.getTyped<RecipeUserInfoApi>(
      '/client-operation/recipe/user/getUserRecipeInfo/{menuId}',
      pathParams: {"menuId": menuId},
      fromJsonT: (json) =>
          RecipeUserInfoApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<void> postDispatchRecipe(
    String menuId,
    String deviceId,
    String electronicControlProtocol,
  ) async {
    final res = await _apiClient.post<void>(
      '/client-operation/recipe/user/pubSmartRecipe',
      data: {
        "menuId": menuId,
        "deviceId": deviceId,
        "electronicControlProtocol": electronicControlProtocol,
      },
    );
    return;
  }

  @override
  Future<ResponseRecipeEcprotocolApi?> getRecipeEcProtocol(
    String recipeId,
    String pid,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeEcprotocolApi>(
      '/client-operation/recipe/platform/toEcProtocol',
      query: {"recipeId": recipeId, "pid": pid},
      fromJsonT: (json) =>
          ResponseRecipeEcprotocolApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<List<RecipeProductApi>?> getRecipeProductListByRecipeId(
    String recipeId,
  ) async {
    final res = await _apiClient.getTyped<List<RecipeProductApi>?>(
      '/client-operation/recipe/platform/selectProductsByMenuId/{recipeId}',
      pathParams: {"recipeId": recipeId},
      fromJsonT: (json) => parseList(json, RecipeProductApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<void> getRecipeUserBrowses(
    int pageNum,
    int pageSize,
    String orderByColumn,
    String isAsc,
  ) async {
    final res = await _apiClient.getTyped<RecipeUserInfoApi>(
      '/client-operation/recipe/user/getUserBrowses',
      query: {
        "pageSize": pageSize,
        "pageNum": 1,
        "orderByColumn": "id",
        "isAsc": "asc",
      },
      fromJsonT: (json) =>
          RecipeUserInfoApi.fromJson(json as Map<String, dynamic>),
    );
    return;
  }

  @override
  Future<ResponseRecipeDetailApi?> getRecipeDetailInfo(String recipeId) async {
    final res = await _apiClient.getTyped<ResponseRecipeDetailApi?>(
      '/client-operation/recipe/platform/getRecipeMenuInfo',
      query: {"id": recipeId},
      fromJsonT: (json) =>
          ResponseRecipeDetailApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<List<MiffyProduct>?> getRecipeMiffyProducts() async {
    final res = await _apiClient.getTyped<List<MiffyProduct>>(
      '/client-product/product/getOlds',
      fromJsonT: (json) => parseList(json, MiffyProduct.fromJson),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeCollectDetailApi?> getRecipeCollectDetailInfo(
    String recipeListInfoId,
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeCollectDetailApi>(
      '/client-operation/recipe/operation/getRecipeListBelongs',
      query: {
        "pageSize": pageSize,
        "pageNum": pageNum,
        "orderByColumn": "sort",
        "recipeListInfoId": recipeListInfoId,
        "isAsc": "asc",
        // "status": 1,
      },
      fromJsonT: (json) =>
          ResponseRecipeCollectDetailApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<CategoryRecipeListApi?> getCategoryRecipeList(
    String id,
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<CategoryRecipeListApi>(
      '/client-operation/recipe/platform/getCategoryRecipeList',
      query: {
        "pageSize": pageSize,
        "pageNum": pageNum,
        "categoryId": id,
        "isAsc": "asc",
      },
      fromJsonT: (json) =>
          CategoryRecipeListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<List<ResponseCookModelApi>?> getCookClassesModules() async {
    final res = await _apiClient.getTyped<List<ResponseCookModelApi>?>(
      '/client-operation/recipe/operation/getCookClassesModules',

      fromJsonT: (json) => parseList(json, ResponseCookModelApi.fromJson),
    );
    return res?.data;
  }

  @override
  Future<ResponseCookModuleContentApi?> getCookModuleContent(
    RequestCookModuleContentApi request,
  ) async {
    final res = await _apiClient.postTyped<ResponseCookModuleContentApi>(
      '/client-operation/recipe/operation/searchAggregate',
      data: request.toJson(),
      fromJsonT: (json) =>
          ResponseCookModuleContentApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  @override
  Future<ResponseRecipeSearchListApi?> getRecipeMenuList(
    String name,
    int pageNum,
    int pageSize,
  ) async {
    final res = await _apiClient.getTyped<ResponseRecipeSearchListApi?>(
      '/client-operation/recipe/platform/selectRecipeMenuList',
      query: {"pageNum": pageNum, "pageSize": pageSize, "name": name},
      fromJsonT: (json) =>
          ResponseRecipeSearchListApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }

  ///////////////////// MIFFY 接口////////////////////
  @override
  Future<ResponseMiffyRecipeCloudListApi?> getMiffyRecipeCloudList(
    RequestMiffyRecipeCloudListApi request,
  ) async {
    final env = getProvider(environmentProvider);
    final res = await _apiClient.postTyped<ResponseMiffyRecipeCloudListApi>(
      '${env.miffy_url}/v1/recipe/getMenuList',
      data: request.toJson(),
      fromJsonT: (json) => ResponseMiffyRecipeCloudListApi.fromJson(
        json as Map<String, dynamic>,
      ),
    );
    return res?.data;
  }

  @override
  Future<ResponseMiffyRecipeInfoApi?> getMiffyRecipeInfo(
    String recipeId,
  ) async {
    final env = getProvider(environmentProvider);
    final res = await _apiClient.getTyped<ResponseMiffyRecipeInfoApi>(
      '${env.miffy_url}/v1/recipe/{recipeId}/getMenuInfo',
      pathParams: {"recipeId": recipeId},
      fromJsonT: (json) =>
          ResponseMiffyRecipeInfoApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data;
  }
}
